import matplotlib.pyplot as plt
import numpy as np

# 数据
years = ['2022', '2023']
debt_to_asset_ratio = [82.01, 80.88]  # 资产负债率（单位：%）
equity_ratio = [16.53, 17.30]  # 股东权益比率（单位：%）

# 设置柱状图的颜色
colors = ['blue', 'orange']

# 创建柱状图
fig, ax1 = plt.subplots()

# 柱状图
bars_debt = ax1.bar(years, debt_to_asset_ratio, color=colors[0], label='Debt to Asset Ratio')

# 折线图
line_equity, = ax1.plot(years, equity_ratio, color=colors[1], marker='o', label='Equity Ratio')

# 在柱体上显示数据
for bar in bars_debt:
    yval = bar.get_height()
    plt.text(bar.get_x() + bar.get_width()/2.0, yval, f'{yval}%', ha='center', va='bottom')

# 设置标题和轴标签
plt.title('Debt to Asset Ratio and Equity Ratio Comparison (2022-2023)')
plt.xlabel('Year')
plt.ylabel('Percentage (%)')

# 创建第二个y轴
ax2 = ax1.twinx()

# 折线图的y轴
ax2.plot(years, equity_ratio, color=colors[1], marker='o', label='Equity Ratio')

# 在折线图上显示数据
for i, txt in enumerate(equity_ratio):
    ax2.annotate(f'{txt}%', (years[i], equity_ratio[i]), textcoords="offset points", xytext=(0,10), ha='center')

ax2.set_ylabel('Equity Ratio (%)')

# 图例
lines, labels = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax2.legend(lines + lines2, labels + labels2, loc='upper left')

# 显示图形
plt.show()